package main

import (
	"fmt"
	"net/http"
	_ "net/http/pprof"
	"os"
	"os/signal"
	"syscall"

	"github.com/gorilla/mux"
	"github.com/sirupsen/logrus"

	"goKit/global"
	"goKit/router"
)

func main() {
	var err error

	// 初始化-日志
	err = global.InitLogger()
	if err != nil {
		logrus.Error("初始化日志失败：", err.Error())
	}

	// 初始化-读取配置文件
	err = global.LoadConfig("conf/conf.yaml")
	if err != nil {
		logrus.Error("读取配置文件失败：", err.Error())
		os.Exit(-1)
	}

	r := mux.NewRouter()
	router.ServiceForHttp(r)

	errChan := make(chan error)
	go (func() {
		logrus.Info("==>> localhost:", global.SystemConfig.Server.ServerHttpPort)
		err := http.ListenAndServe(":"+global.SystemConfig.Server.ServerHttpPort, r)
		if err != nil {
			logrus.Error(err)
			errChan <- err
		}
	})()

	// 监听，服务注销
	go (func() {
		sigChannel := make(chan os.Signal)
		signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGCONT)
		errChan <- fmt.Errorf("%s", <-sigChannel)
	})()

	// 监视器 http://localhost:6060/debug/pprof/  https://blog.csdn.net/qfzhangwei/article/details/121385756
	go http.ListenAndServe("0.0.0.0:6060", nil)

	getErr := <-errChan
	fmt.Println(getErr)
}
